home *** CD-ROM | disk | FTP | other *** search
/ Chip 1997 October / CD CHIP.ISO / WebServ / server7 / bin / Pager.dpr < prev    next >
Encoding:
Text File  |  1996-07-07  |  6.6 KB  |  241 lines

  1. library Pager;
  2.  
  3. uses
  4.   SysUtils,
  5.   Classes,
  6.   Httpext,
  7.   ISAPISock,
  8.   Parser,
  9.   SocketComm;
  10.  
  11. const
  12.   SMTPHost='smtp.myhost.com';                // Address of SMTP service
  13.   SMTPPort=25;
  14.   PAGINGACCOUNT='pagermail@myhost.com';      // Account to receive mail requesting page
  15.   USERNAME='justme';                         // User name needed for SMTP transaction
  16.  
  17.  
  18. //
  19. // Uses SMTP to send a mail message to the paging server. The
  20. // subject line reads SUBJECT: P:xxxx yyyy
  21. // where xxxx is the pager number and yyyy is the number to be
  22. // displayed.
  23. //  
  24. function DoPage(PagerNumber, Msg: String): Boolean;
  25. var
  26.   socket: TBSocketComm;
  27. begin
  28.   // Indicate success unless we find out otherwise...
  29.   Result:=True;
  30.   begin
  31.     try
  32.       socket:=TBSocketComm.Create;
  33.       socket.SetTimeOut(30000);
  34.       socket.Connect(SMTPHost, SMTPPort);
  35.       socket.SetTXTerm(#13#10);
  36.       socket.SetRXTerm(#13#10);
  37.  
  38.       try
  39.         // Start speaking SMTP to get the mail message out. Note
  40.         // if any of these calls fail, we'll still free the socket.
  41.         socket.Writeln('MAIL FROM: '+USERNAME);
  42.         socket.Writeln('RCPT TO: '+PAGINGACCOUNT);
  43.         socket.Writeln('DATA');
  44.         socket.Writeln('SUBJECT: P:'+PagerNumber+' '+Msg);
  45.         socket.Writeln('.');
  46.         socket.Writeln('QUIT');
  47.         socket.Close;
  48.       finally
  49.         socket.Free;
  50.       end;
  51.     except
  52.        // Something bad happened during the mail transaction with the
  53.        // SMTP server. We assume the message didn't get out and flag
  54.        // the function as having failed.
  55.        Result:=False;
  56.     end
  57.   end
  58. end;
  59.  
  60. //
  61. // Called whenever a GET is performed with a query string. This
  62. // is most always due to someone submitting a page.
  63. //
  64. procedure PageSubmitted(sock: TISAPISock);
  65. var
  66.   ntbd: String;
  67.   pagerNumber: String;
  68.   query: String;
  69. begin
  70.   with sock do
  71.   begin
  72.     HHeader('PageBoy Page Status', hcLtGray, hcBlack, hcBlue);
  73.     HPageStart;
  74.  
  75.     // Read what was submitted
  76.     query:=GetServerVariable('QUERY_STRING');
  77.  
  78.     // Read the Number To Be Displayed as a cookie
  79.     ntbd:=GetCookieVal('MyNumber');
  80.  
  81.     // Parse the pager number out of the query string
  82.     PagerNumber:=GetToken( query, 2, ['(', ')']);
  83.  
  84.     // If we don't have all the info we need, we fail.
  85.     if ntbd='' then
  86.       HLine('Page to '+query+' Failed! The cookie was invalid.')
  87.     else if pagerNumber='' then
  88.       HLine('Page to '+query+' Failed! The pager was invalid.')
  89.     else if DoPage(pagerNumber, ntbd) then
  90.       HLine('Page to '+query+' accepted!')
  91.     else
  92.       HLine('Page to '+query+' Failed! The SMPT transaction failed.');
  93.  
  94.     HPageEnd;
  95.   end;
  96. end;
  97.  
  98. //
  99. // Called anytime a GET is performed on this DLL
  100. //
  101. procedure ProcessGet(sock: TISAPISock);
  102. var
  103.   fin: TextFile;
  104.   s: String;
  105. begin
  106.  with sock do
  107.  begin
  108.    // Blast out a header
  109.    Writeln('HTTP/1.0 200 OK');
  110.    Writeln('Content-type: text/html');
  111.    Writeln('Expires: 0');
  112.    Writeln('');
  113.  
  114.    // If there is any query string, then the user
  115.    // is submitting a page.
  116.    if GetServerVariable('QUERY_STRING')<>'' then
  117.      PageSubmitted(sock)
  118.    else
  119.    begin
  120.      // Here a raw GET with out any query string has been
  121.      // submitted. Blast out everything we know about the database
  122.      // and the user's cookie.
  123.      HHeader('PageBoy: Remote Page', hcLtGray, hcBlack, hcBlue);
  124.      HPageStart;
  125.  
  126.      HSeparator;
  127.      HImage( 'pageboy.gif' );
  128.      HHeading(1,'PageBoy: Remote Page');
  129.  
  130.      // If user hits submit, we'll change his cookie
  131.      HFormStart('POST', '/bin/Pager.dll');
  132.  
  133.      // Setup an edit box with the number to be displayed. This cookie
  134.      // can be changed if the user hits the submit button.
  135.      HSeparator;
  136.      HLine( HItalic( HBold('NOTE:')+ ' If your browser supports "cookies", then a cookie will be added when you press "Change Number...". A cookie is simply a piece of information the server can request the browser to maintain until the next session.'));
  137.      HEditBox('Number to be displayed: ', 'MyNumber', GetCookieVal('MyNumber'), 15, 15);
  138.      HFormEnd('Change Number to be Displayed','');
  139.      HSeparator;
  140.  
  141.      HLine( HBold('Select person to page') );
  142.      // List out all the names of people in the
  143.      // database. If an error occurs, we'll send that
  144.      // to the user.
  145.      try
  146.        AssignFile(fin, ExtractFilePath(GetServerVariable('SCRIPT_NAME'))+'database.txt');
  147.        reset(fin);
  148.      try
  149.        while NOT Eof(fin) do
  150.        begin
  151.          System.Readln(fin, s);
  152.          HLine( HRef('/bin/Pager.dll?'+EscapeEncode(s), s) );
  153.        end;
  154.      finally
  155.        CloseFile(fin);
  156.      end;
  157.      except
  158.        HLine('A problem occurred reading file '+ExtractFilePath(GetServerVariable('SCRIPT_NAME'))+'database.txt');
  159.      end;
  160.      HSeparator;
  161.      HPageEnd;
  162.    end;
  163.  end;
  164. end;
  165.  
  166. //
  167. // Called in response to the user wishing to change
  168. // his Number To Be Displayed. This will update the
  169. // cookie
  170. //
  171. procedure ProcessPost(sock: TISAPISock);
  172. var
  173.   myNumber: String;
  174. begin
  175.   with sock do
  176.   begin
  177.     Writeln('HTTP/1.0 200 OK');
  178.     Writeln('Content-type: text/html');
  179.     Writeln('Expires: 0');
  180.  
  181.     // Read the form value
  182.     myNumber:=GetFormVal('MyNumber');
  183.     // Create a cookie and make it expire in a month
  184.     ClearCookie('MyNumber');
  185.     SetCookie('MyNumber', myNumber, 28);
  186.     Writeln('');
  187.  
  188.     HHeader('', hcLtGray, hcBlack, hcBlue);
  189.     HLine('Number to be displayed is now: '+myNumber);
  190.     HLine('Your browser has been updated.');
  191.   end;
  192. end;
  193.  
  194. // CASE MATTERS FOR THIS FUNCTION NAME
  195. function GetExtensionVersion(var ver: THSE_VERSION_INFO): Boolean; stdcall;
  196. begin
  197.   result:=True;
  198. end;
  199.  
  200. // CASE MATTERS FOR THIS FUNCTION NAME
  201. function HttpExtensionProc(var ecb: TEXTENSION_CONTROL_BLOCK): LongInt; stdcall;
  202. var
  203.   sock: TISAPISock;
  204.   method: String;
  205. begin
  206.   // Create the socket helper
  207.   sock:=TISAPISock.Create(ecb);
  208.  
  209.   method:=sock.GetServerVariable('REQUEST_METHOD');
  210.   if method='GET' then
  211.     ProcessGet(sock)
  212.   else if method='POST' then
  213.     ProcessPost(sock)
  214.   else
  215.   begin
  216.     sock.Writeln('HTTP/1.0 200 OK');
  217.     sock.Writeln('Content-type: text/html');
  218.     sock.Writeln('');
  219.     sock.Writeln('I didn''t understand that request');
  220.   end;
  221.  
  222.  
  223.   // Return a normal status code
  224.   StrLCopy( ecb.lpszLogData, PChar('DLL Finished with no errors'), HSE_LOG_BUFFER_LEN-1);
  225.   Result:=HSE_STATUS_SUCCESS;
  226.  
  227.   // Free the socket
  228.   sock.Free;
  229. end;  
  230.  
  231. // * REQUIRED FOR DYNAMIC BINDING.
  232. // * Index values aren't need.
  233. // * Case doesn't matter here.
  234. exports
  235.   GetExtensionVersion,
  236.   HttpExtensionProc;
  237.   
  238. begin
  239. end.
  240.  
  241.